<?php
/**
 * Created by PhpStorm.
 * User: sunyang
 * Date: 2017/1/21
 * Time: 11:39
 */
class FtpLib{
    var $_uploader=null;
    function __construct()
    {
        $this->ftpbase();
    }
    public function ftpbase(){
        static $uploader = null;
        if($uploader === null){
            $ftp_server = "192.168.10.72";
            $ftp_user = "imagesdss";
            $ftp_pass = "abc.123";
            include_once(ROOTPATH . '/lib/include/ftp_server.lib.php');
            $uploader = new FtpServer($ftp_server);
            $uploader ->login($ftp_user,$ftp_pass);
            $uploader ->pasv(true);
            $this->_uploader = $uploader;
        }
    }
    //把本地文件移动到ftp服务器
    public function ftp_move_file($arr){
        $local_url = $arr['local_url'];
        $ftp_url = $arr['ftp_url'];
        $ftp_url = trim($ftp_url,'/');
        $folders = explode('/',$ftp_url);
        $count = count($folders);
        //进入目录，如果进不去则创建目录
        $i=0;
        $bool = 0;
        foreach ($folders as $d)
        {
            $i++;
            if(empty($d)){
                continue;
            }
            if($i==$count){
                $bool = $this->_uploader ->put($local_url,$d);
            }elseif (!$this->_uploader->chdir($d))
            {

                $this->_uploader->mkdir($d);
                $this->_uploader->chmod($d);
                $this->_uploader->chdir($d);
            }
        }
        return $bool;
    }
    //关闭ftp服务器
    public function close(){
        $this->_uploader->close();
    }
}